﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;


public partial class Webusercontrol_khaibaochuongtrinhkhuyenmai : System.Web.UI.UserControl
{
    
    string str = BCDataCenter.Configuration.CurrentConnectionString;
    string error = "";
    ClassExecute.ClassExecute dt;
    static bool flag = false;
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }
    protected void bt_taomoi_Click(object sender, EventArgs e)
    {
        flag = true;
        txt_ID.Text = "";
        txt_ID.ReadOnly = false;
        txt_proDes.Text = "";
        txt_fromdate.Text = "";
        txt_todate.Text = DateTime.Now.ToString("dd/MM/yyyy");
        txtMoreDes.Text = "";

    }
    protected void bt_Luu_Click(object sender, EventArgs e)
    {
        dt = new ClassExecute.ClassExecute();

        if (flag)
        {
            int i = dt.ExecuteSQl_int(str, "k_insertARPRODEF", CommandType.StoredProcedure, ref error,
                new SqlParameter("@Program_ID", txt_ID.Text),
                new SqlParameter("@Program_Des", txt_proDes.Text),
                new SqlParameter("@From", txt_fromdate.Text),
                new SqlParameter("@To", txt_todate.Text),
                new SqlParameter("@Active", chActive.Checked),
                new SqlParameter("@MoreDesc", txtMoreDes.Text));
            if (i == 1)
            {
                uscMsgBox1.AddMessage("Mã chương trình khuyến mãi này đã trùng,chọn mã khác", uscMsgBox.enmMessageType.Error);
            }
            else
            {
                uscMsgBox1.AddMessage("Khai báo chương trình khuyến mãi thành công", uscMsgBox.enmMessageType.Success);
                flag = true;
                txt_ID.ReadOnly = true;
            }
        }
        else
        {
            int i = dt.ExecuteSQl_int(str, "k_updateARPRODEF", CommandType.StoredProcedure, ref error,
               new SqlParameter("@Program_ID", txt_ID.Text),
               new SqlParameter("@Program_Des", txt_proDes.Text),
               new SqlParameter("@From", txt_fromdate.Text),
               new SqlParameter("@To", txt_todate.Text),
               new SqlParameter("@Active", chActive.Checked),
               new SqlParameter("@MoreDesc", txtMoreDes.Text));
            if (i == 1)
            {
                uscMsgBox1.AddMessage("Mã chương trình khuyến mãi này không tồn tại", uscMsgBox.enmMessageType.Error);
            }
            else
            {
                uscMsgBox1.AddMessage("Cập nhật chương trình khuyến mãi thành công", uscMsgBox.enmMessageType.Success);
               
            }
        }
    }  
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        FindData();
    }
    void FindData()
    {
        dt = new ClassExecute.ClassExecute();
        DataTable db = dt.Get_Data_DataTable(str, ref error, "k_findARPRODEF", CommandType.StoredProcedure,
            new SqlParameter("@From", txtFromDate.Text),
            new SqlParameter("@To", txtToDate.Text));
        if (db.Rows.Count > 0)
        {
            GridView2.DataSource = db;
            GridView2.DataBind();
        }
    }
    protected void GridView2_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView2.PageIndex = e.NewPageIndex;
        FindData();

    }
    protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "chon")
        {
            GridViewRow grv = ((Control)e.CommandSource).NamingContainer as GridViewRow;
            dt = new ClassExecute.ClassExecute();

            try
            {
                DataTable db = dt.Get_Data_DataTable(str, ref error, "k_GetARPRODEF", CommandType.StoredProcedure,
                    new SqlParameter("@Program_ID", grv.Cells[0].Text));
                if (db.Rows.Count > 0)
                {
                    txt_ID.Text = db.Rows[0]["Program_ID"].ToString();
                    txt_proDes.Text = db.Rows[0]["Program_Des"].ToString();
                    txt_fromdate.Text = db.Rows[0]["From"].ToString();
                    txt_todate.Text = db.Rows[0]["To"].ToString();
                    txtMoreDes.Text = db.Rows[0]["MoreDesc"].ToString();
                    chActive.Checked = bool.Parse(db.Rows[0]["Active"].ToString());
                    txt_ID.ReadOnly = true;
                    flag = false;
                }
            }
            catch(Exception ex)
            {
                BCDataCenter.ClassWriteLogErr obj = new BCDataCenter.ClassWriteLogErr();
                string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
                obj.ErrorLog(ex.Message, method);
            }
        }
    }
    
}
